/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.je.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.redragon.framework.springmvc.ControllerSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import com.erp.ledger.je.dao.model.GLJeModelLine;
import com.erp.ledger.je.dao.model.GLJeModelLineCO;
import com.erp.ledger.je.service.GLJeModelLineService;

@Controller
@RequestMapping("/web/gLJeModelLine")
public class GLJeModelLineWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(GLJeModelLineWebController.class);
    
    //服务层注入
    @Autowired
    private GLJeModelLineService gLJeModelLineService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getGLJeModelLineList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-01-30 14:59:47
     * @author 
     * @param pages
     * @param gLJeModelLineCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeModelLineList")
    public String getGLJeModelLineList(Pages pages, GLJeModelLineCO gLJeModelLineCO, Model model) {
        //TODO: 分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(10);
        pages.setShowPageNum(7);
        pages.setStartEndPageNum(2);
        
        //分页查询数据
        List<GLJeModelLine> gLJeModelLineList = this.gLJeModelLineService.getDataObjects(pages, gLJeModelLineCO);
        
        //页面属性设置
        model.addAttribute("gLJeModelLineList", gLJeModelLineList);
        model.addAttribute("pages", pages);
        
        return "gLJeModelLine/gLJeModelLineList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-01-30 14:59:47
     * @author 
     * @param gLJeModelLine
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeModelLine")
    public String getGLJeModelLine(GLJeModelLine gLJeModelLine, Model model) {
        //TODO: 查询要编辑的数据
        
        //页面属性设置
        model.addAttribute("gLJeModelLine", gLJeModelLine);
        
        return "gLJeModelLine/gLJeModelLineEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-01-30 14:59:47
     * @author 
     * @param gLJeModelLine
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editGLJeModelLine")
    public String editGLJeModelLine(@Valid GLJeModelLine gLJeModelLine, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getGLJeModelLine";
        }
        
        //TODO: 对当前编辑的对象初始化默认的字段
        
        //保存编辑的数据
        this.gLJeModelLineService.insertOrUpdateDataObject(gLJeModelLine);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        
        return "redirect:getGLJeModelLineList";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-01-30 14:59:47
     * @author 
     * @param gLJeModelLine
     * @return String
     *
     */
    @RequestMapping("deleteGLJeModelLine")
    public String deleteGLJeModelLine(GLJeModelLine gLJeModelLine, String jeHeadId, RedirectAttributes attr) {
        //删除数据前验证数据
        if(gLJeModelLine!=null&&gLJeModelLine.getJeLineId()!=null) {
            //删除数据
            this.gLJeModelLineService.deleteDataObject(gLJeModelLine);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("jeHeadCode", gLJeModelLine.getJeHeadCode());
            attr.addAttribute("jeHeadId", jeHeadId);
        }
        
        return "redirect:/web/gLJeModelHead/getGLJeModelHead";
    }
}
